package com.gomtel.smartdevice.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchBaseFragemnt;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.global.WatchApplication;
import com.gomtel.smartdevice.moudle.db.GreenDaoHelper;
import com.gomtel.smartdevice.moudle.db.entity.AppConfigurationInfo;
import com.gomtel.smartdevice.moudle.db.entity.DaoSession;
import com.gomtel.smartdevice.moudle.db.entity.LoginUserInfo;
import com.gomtel.smartdevice.moudle.db.entity.SignsOfDataInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.ui.activity.BPCActivity;
import com.gomtel.smartdevice.ui.activity.MultiTypeActivity;
import com.gomtel.smartdevice.ui.activity.PersonageActivtiy;
import com.gomtel.smartdevice.ui.activity.SettingActivity;
import com.gomtel.smartdevice.ui.activity.tools.DeviceConnectionActivity;
import com.gomtel.smartdevice.ui.dialog.ShowDialog;
import com.gomtel.smartdevice.ui.view.CircleImageView;

import java.text.DecimalFormat;

import butterknife.BindView;

import static com.gomtel.smartdevice.global.DataClass.mStringAndBitmap;

/**
 * Created by Administrator on 2018/5/14 0014.
 */

public class PersonageFragment extends WatchBaseFragemnt implements View.OnClickListener {

    @BindView(R.id.userName)
    TextView userName;
    @BindView(R.id.photo_view)
    CircleImageView photo_view;
    @BindView(R.id.user_height)
    TextView user_height;
    @BindView(R.id.user_weight)
    TextView user_weight;
    @BindView(R.id.user_bmi)
    TextView user_bmi;
    @BindView(R.id.user_target_number)
    TextView user_target_number;

    @BindView(R.id.layout_mydevice)
    RelativeLayout layout_mydevice;
    @BindView(R.id.mydevice_status)
    View mydevice_status;
    @BindView(R.id.mydevice_name)
    TextView mydevice_name;

    @BindView(R.id.layout_setting)
    RelativeLayout layout_setting;

    @BindView(R.id.layout_pressure_calibration)
    RelativeLayout layout_pressure_calibration;

    @BindView(R.id.or_invisibility)
    LinearLayout or_invisibility;

    @BindView(R.id.layout_calibration_off)
    RelativeLayout layout_calibration_off;
    @BindView(R.id.layout_clear_calibration)
    RelativeLayout layout_clear_calibration;

    @BindView(R.id.layout_clear_cache)
    RelativeLayout layout_clear_cache;

    @BindView(R.id.layout_usinghelp)
    RelativeLayout layout_usinghelp;

    @BindView(R.id.layout_add_device)
    RelativeLayout layout_add_device;

    @BindView(R.id.add_device)
    AppCompatCheckBox add_device;

    private boolean OrLayoutStatus = true;
    private ShowDialog showDialog;
    private SignsOfDataInfo signsOfDataInfo;
    private int height;
    private int weight;
    private int stepCountStandard;
    private String bmi;
    private AppConfigurationInfo appConfigurationInfo;
    private LoginUserInfo loginUserInfo;

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {
            case EventCode.PERSONAGE_UP_DATA:
                initData();
                initView();
                break;
            case EventCode.OPEN_CLEAR_CACHE:
                if (dataManager != null && WatchApplication.appComponent != null) {
                    GreenDaoHelper greenDaoHelper = WatchApplication.appComponent.greenDaoHelper();
                    if (greenDaoHelper != null) {
                        DaoSession daoSession = greenDaoHelper.getmDaoSession();
                        if (daoSession != null) {
                            daoSession.getAtrialFibrillationInfoDao().deleteAll();
                            daoSession.getBdDataInfoDao().deleteAll();
                            daoSession.getCaloryDataInfoDao().deleteAll();
                            daoSession.getEcgDataInfoDao().deleteAll();
                            daoSession.getHeartRateDataInfoDao().deleteAll();
                            daoSession.getStepCountInfoDao().deleteAll();
                            daoSession.getStepCountContentInfoDao().deleteAll();
                            daoSession.getPulseConditionBeanDao().deleteAll();
                            daoSession.getPathLengthContentInfoDao().deleteAll();
                            daoSession.getPathLengthInfoDao().deleteAll();
                            Log.e(TAG, "清除缓存成功");
                        }
                    }
                }
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_personage;
    }

    @Override
    protected void initClass() {
        showDialog = ShowDialog.newInstance();
    }

    @Override
    protected void initData() {
        loginUserInfo = dataManager.queryLoginUserInfo(UserContent.UserName);
        signsOfDataInfo = dataManager.querySignsOfDataInfo(UserContent.UserName);
        appConfigurationInfo = dataManager.queryAppConfigurationInfo(UserContent.UserName);
        height = signsOfDataInfo.getHeight();
        weight = signsOfDataInfo.getWeight();
        stepCountStandard = signsOfDataInfo.getStepCountStandard();
        bmi = Bmi(weight, height);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        if (signsOfDataInfo != null) {
            if (signsOfDataInfo.getUserPhoto() != null) {
                photo_view.setImageBitmap(mStringAndBitmap.stringToBitmap(signsOfDataInfo.getUserPhoto()));
            } else {
                Glide.with(this).load(loginUserInfo.getUserPhoto()).asBitmap().into(photo_view);
            }
            if (signsOfDataInfo.getNickName() != null) {
                userName.setText(signsOfDataInfo.getNickName());
            } else {
                userName.setText(loginUserInfo.getNickName());
            }
        } else {
            userName.setText(loginUserInfo.getNickName());
            Glide.with(this).load(loginUserInfo.getUserPhoto()).asBitmap().into(photo_view);
        }
        user_height.setText(String.valueOf(height) + "cm");
        user_weight.setText(String.valueOf(weight) + "kg");
        user_bmi.setText(bmi);
        user_target_number.setText(String.valueOf(stepCountStandard));
        if (appConfigurationInfo != null) {
            mydevice_name.setText(appConfigurationInfo.getDeviceName());
            layout_mydevice.setVisibility(View.VISIBLE);
            layout_add_device.setVisibility(View.GONE);
        } else {
            layout_mydevice.setVisibility(View.GONE);
            layout_add_device.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initListener() {
        layout_pressure_calibration.setOnClickListener(this);
        photo_view.setOnClickListener(this);
        layout_clear_calibration.setOnClickListener(this);
        layout_calibration_off.setOnClickListener(this);
        layout_clear_cache.setOnClickListener(this);
        layout_setting.setOnClickListener(this);
        layout_usinghelp.setOnClickListener(this);
        add_device.setOnClickListener(this);
        layout_mydevice.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_pressure_calibration:
                if (OrLayoutStatus) {
                    or_invisibility.setVisibility(View.VISIBLE);
                } else {
                    or_invisibility.setVisibility(View.GONE);
                }
                OrLayoutStatus = !OrLayoutStatus;
                break;
            case R.id.photo_view:
                startActivity(new Intent(getActivity(), PersonageActivtiy.class));
                break;
            case R.id.layout_clear_calibration:
                showDialog.showStatusDialog(getActivity(), EventCode.OPEN_CLEAR_CALIBRATION);
                break;
            case R.id.layout_calibration_off:
                startActivity(new Intent(getActivity(), BPCActivity.class));
                break;
            case R.id.layout_clear_cache:
                showDialog.showStatusDialog(getActivity(), EventCode.OPEN_CLEAR_CACHE);
                break;
            case R.id.layout_mydevice:
                Intent intent = new Intent(getActivity(), MultiTypeActivity.class);
                intent.putExtra("ActionType", String.valueOf(EventCode.UNBUNDLE));
                startActivity(intent);
                break;
            case R.id.layout_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.layout_usinghelp:
                Intent intentType = new Intent(getActivity(), MultiTypeActivity.class);
                intentType.putExtra("ActionType", String.valueOf(EventCode.WEB));
                startActivity(intentType);
                break;
            case R.id.add_device:
                startActivity(new Intent(getActivity(), DeviceConnectionActivity.class));
                break;
        }
    }

    private String Bmi(int weight, int heigth) {
        DecimalFormat df = new DecimalFormat("0.00");
        Double format_height = Double.valueOf(df.format((float) heigth / 100));
        return String.valueOf(df.format((float) weight / (format_height * 2)));
    }
}
